package com.java.springcloud.mybatisplus.injector.methods;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

import java.util.Map;

/**
 * 查询满足条件所有数据
 *
 * @author zhangtao
 * @since 2018-04-06
 */
public class TdSelectMaps extends AbstractMethod {

    private static final String TD_SELECT_MAPS = "<script>\nSELECT %s FROM %s %s %s\n</script>";
    private String WRAPPER_TABLE_SUFFIX = WRAPPER_DOT + "tableSuffix";

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String tableName = tableInfo.getTableName() + SqlScriptUtils.unSafeParam(WRAPPER_TABLE_SUFFIX);
        String sql = String.format(TD_SELECT_MAPS, sqlSelectColumns(tableInfo, true),
                tableName, sqlWhereEntityWrapper(true, tableInfo),
                sqlComment());
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addSelectMappedStatementForOther(mapperClass, "tdSelectMaps", sqlSource, Map.class);
    }
}
